//
//  ToggleSegmentedControl.m
//  Segment
//
//  Created by nhn on 14-7-10.
//  Copyright (c) 2014年 CNUIC. All rights reserved.
//

#import "ToggleSegmentedControl.h"

@implementation ToggleSegmentedControl

- (void)initIndex
{
	selectedSegment = self.selectedSegmentIndex;
	previousSelectedSegment = self.selectedSegmentIndex;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
		_selectlessAble = true;
		[self initIndex];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	self = [super initWithCoder:aDecoder];
	if (self) {
		[self initIndex];
	}
	return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	previousSelectedSegment = selectedSegment;
	[super touchesBegan:touches withEvent:event];
}

- ( void )touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
	
    if ( selectedSegment == self.selectedSegmentIndex && selectedSegment != UISegmentedControlNoSegment && self.selectlessAble)
    {
        self.selectedSegmentIndex = UISegmentedControlNoSegment;
        selectedSegment = UISegmentedControlNoSegment;
		[self sendActionsForControlEvents:UIControlEventValueChanged];
    } else {
        selectedSegment = self.selectedSegmentIndex;
    }
}

- (NSInteger)previousSelectedSegmentIndex
{
	return previousSelectedSegment;
}

@end
